package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.model.ShopLink;
import com.yami.shop.service.ShopLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 店铺链接控制器
 */
@RestController
@RequestMapping("/shopLink")
@Api(tags = "店铺链接接口", description = "提供店铺链接相关的增删改查接口")
public class ShopLinkController {

    @Resource
    private ShopLinkService shopLinkService;

    /**
     * 更新店铺链接
     */
    @PutMapping
    @ApiOperation(value = "更新店铺链接", notes = "更新店铺链接信息")
    public ResponseEntity<Void> updateShopLink(@RequestBody ShopLink shopLink) {
        return shopLinkService.updateShopLink(shopLink);
    }

    /**
     * 获取店铺链接
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取店铺链接", notes = "根据ID获取店铺链接详情")
    public ResponseEntity<ShopLink> getShopLinkById(
            @ApiParam(value = "链接ID", required = true, example = "1") @PathVariable Long id) {
        return shopLinkService.getShopLinkById(id);
    }

    /**
     * 根据店铺ID获取店铺链接
     */
    @GetMapping("/shop/{shopId}")
    @ApiOperation(value = "根据店铺ID获取链接", notes = "根据店铺ID获取店铺链接详情")
    public ResponseEntity<ShopLink> getShopLinkByShopId(
            @ApiParam(value = "店铺ID", required = true, example = "1") @PathVariable Long shopId) {
        return shopLinkService.getShopLinkByShopId(shopId);
    }

    /**
     * 分页查询店铺链接
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询店铺链接", notes = "分页查询店铺链接列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "shopId", value = "店铺ID", paramType = "query", dataType = "Long"),
        @ApiImplicitParam(name = "status", value = "链接状态", paramType = "query", dataType = "Integer")
    })
    public ResponseEntity<IPage<ShopLink>> listShopLinksByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long shopId,
            @RequestParam(required = false) Integer status) {
        Page page = new Page(pageNum, pageSize);
        return shopLinkService.listShopLinksByPage(page, shopId, status);
    }

    /**
     * 更新店铺链接状态
     */
    @PutMapping("/{id}/status/{status}")
    @ApiOperation(value = "更新店铺链接状态", notes = "更新店铺链接的状态")
    public ResponseEntity<Void> updateShopLinkStatus(
            @ApiParam(value = "链接ID", required = true, example = "1") @PathVariable Long id,
            @ApiParam(value = "链接状态", required = true, example = "1") @PathVariable Integer status) {
        return shopLinkService.updateShopLinkStatus(id, status);
    }

    /**
     * 生成店铺短链接
     */
    @PostMapping("/generate/{shopId}")
    @ApiOperation(value = "生成店铺短链接", notes = "为指定店铺生成短链接")
    public ResponseEntity<ShopLink> generateShopShortLink(
            @ApiParam(value = "店铺ID", required = true, example = "1") @PathVariable Long shopId) {
        return shopLinkService.generateShopShortLink(shopId);
    }
} 